﻿package mortal.game.scene3D.display3d.icon3d
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import flash.display3D.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.core.*;
    import frEngine.event.*;
    import frEngine.math.*;
    import frEngine.render.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.registType.*;
    import mortal.game.scene3D.*;
    import mortal.game.scene3D.layer3D.utils.*;

    public class Icon3DRender extends DefaultRender
    {
        private var _targeticonSurface:FrSurface3D;
        private var _iconMaterial:ShaderBase;
        private var _iconPosListRegister:VcParam;
        private var _iconTextureRegister:FsParam;
        private static var _instance:Icon3DRender;
        public static const gridNum:uint = 8;
        public static const imgW:uint = 256;
        public static const imgH:uint = 256;

        public function Icon3DRender()
        {
            if (_instance && _instance != this)
            {
                return;
            }
            _instance = this;
            return;
        }// end function

        public function init(param1:GameScene3D) : void
        {
            this.initicons();
            this.initMaterial(param1);
            return;
        }// end function

        public function reinit(param1:GameScene3D) : void
        {
            return;
        }// end function

        private function initMaterial(param1:GameScene3D) : void
        {
            var _loc_2:* = Global3D.nullBitmapDataTexture;
            this._iconMaterial = new ShaderBase("_iconMaterial", new Icon3DVertexFilter(), new Icon3DFragmentFilter(_loc_2));
            this._iconMaterial.materialParams.setBlendMode(EBlendType.BLEND_ALPHA0);
            this._iconMaterial.materialParams.depthWrite = false;
            this._iconMaterial.materialParams.depthCompare = Context3DCompareMode.ALWAYS;
            this._iconMaterial.materialParams.twoSided = true;
            FrEventDispatcher.instance.proxyAddEventListener(this._iconMaterial, Engine3dEventName.MATERIAL_REBUILDER, this.reBuildHander);
            this._iconMaterial.buildBytes();
            return;
        }// end function

        private function reBuildHander(event:Event) : void
        {
            this._iconPosListRegister = this._iconMaterial.getParam("{iconPosList}", true);
            this._iconTextureRegister = this._iconMaterial.getParam("{iconBg0}", false);
            FrEventDispatcher.instance.proxyRemoveEventListener(this._iconMaterial, Engine3dEventName.MATERIAL_REBUILDER, this.reBuildHander);
            return;
        }// end function

        private function initicons() : void
        {
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            this._targeticonSurface = new FrSurface3D("icons");
            var _loc_1:* = this._targeticonSurface.addVertexData(FilterName_ID.POSITION_ID, 3, true, null);
            this._targeticonSurface.addVertexDataToTargetBuffer(FilterName_ID.UV_ID, 2, null, _loc_1);
            var _loc_2:* = imgW / gridNum / 2;
            var _loc_3:* = imgH / gridNum / 2;
            var _loc_4:* = this.createSinglePlaneVector(_loc_2, _loc_3);
            var _loc_5:* = new Vector.<Number>;
            var _loc_6:* = this._targeticonSurface.addVertexData(FilterName_ID.SKIN_INDICES_ID, 2, true, null);
            var _loc_7:* = _loc_6.vertexVector;
            var _loc_8:int = 0;
            while (_loc_8 < 50)
            {
                
                _loc_5 = _loc_5.concat(_loc_4);
                _loc_10 = _loc_8 * 2;
                _loc_11 = _loc_10 + 1;
                _loc_7.push(_loc_10, _loc_11, _loc_10, _loc_11, _loc_10, _loc_11, _loc_10, _loc_11);
                _loc_8++;
            }
            _loc_1.vertexVector = _loc_5;
            var _loc_9:* = new Vector.<uint>;
            _loc_8 = 0;
            _loc_10 = 0;
            while (_loc_8 < 50)
            {
                
                _loc_9.push(_loc_10, _loc_10 + 2, (_loc_10 + 1));
                _loc_9.push(_loc_10 + 3, _loc_10 + 2, _loc_10);
                _loc_10 = _loc_10 + 4;
                _loc_8++;
            }
            this._targeticonSurface.indexVector = _loc_9;
            return;
        }// end function

        private function createSinglePlaneVector(param1:Number, param2:Number) : Vector.<Number>
        {
            var _loc_3:* = new Vector.<Vector3D>;
            _loc_3.push(new Vector3D(-param1, -param2, 0));
            _loc_3.push(new Vector3D(param1, -param2, 0));
            _loc_3.push(new Vector3D(param1, param2, 0));
            _loc_3.push(new Vector3D(-param1, param2, 0));
            var _loc_4:* = new Vector.<Point>;
            var _loc_5:Number = 1;
            _loc_4.push(new Point(0, _loc_5));
            _loc_4.push(new Point(_loc_5, _loc_5));
            _loc_4.push(new Point(_loc_5, 0));
            _loc_4.push(new Point(0, 0));
            var _loc_6:* = Scene3DUtil.cameraQuaternion;
            var _loc_7:* = new Vector.<Number>;
            var _loc_8:* = new Vector3D();
            var _loc_9:int = 0;
            while (_loc_9 < 4)
            {
                
                _loc_6.rotatePoint(_loc_3[_loc_9], _loc_8);
                _loc_7.push(_loc_8.x, _loc_8.y, _loc_8.z, _loc_4[_loc_9].x, _loc_4[_loc_9].y);
                _loc_9++;
            }
            return _loc_7;
        }// end function

        override public function draw(param1:Mesh3D, param2:ShaderBase = null) : void
        {
            var _loc_7:Vector.<Number> = null;
            if (!this._iconPosListRegister)
            {
                return;
            }
            if (!this._iconMaterial.hasPrepared(param1, this._targeticonSurface))
            {
                return;
            }
            var _loc_3:* = this._iconMaterial.materialParams;
            var _loc_4:* = Icon3DMesh(param1).listArr;
            var _loc_5:* = _loc_4.length;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_5)
            {
                
                var _loc_8:* = Global3D;
                var _loc_9:* = Global3D.objectsDrawn + 1;
                _loc_8.objectsDrawn = _loc_9;
                _loc_7 = _loc_4[_loc_6];
                this._iconPosListRegister.value = _loc_7;
                this._iconTextureRegister.value = Icon3DMesh(param1).iconBg0;
                this._iconMaterial.draw(param1, this._targeticonSurface, _loc_3.depthCompare, _loc_3.cullFace, false, _loc_3.sourceFactor, _loc_3.destFactor, 0, _loc_7.length * 0.25);
                _loc_6++;
            }
            return;
        }// end function

        public static function get instance() : Icon3DRender
        {
            if (!_instance)
            {
                new Icon3DRender;
            }
            return _instance;
        }// end function

    }
}
